<template>
  <el-form ref="ruleFormPasswordRef" :rules="rules" :model="form" label-position="right" label-width="90px">
    <el-form-item label="当前密码" prop="passwordOld">
      <el-input id="passwordOld" v-model="form.passwordOld" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="新密码" prop="passwordNew">
      <el-input id="passwordNew" v-model="form.passwordNew" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="确认密码" prop="passwordNew2">
      <el-input id="passwordNew2" v-model="form.passwordNew2" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item>
      <el-button icon="el-icon-refresh" @click="resetPassword">重 置</el-button>
      <el-button icon="el-icon-check" type="primary" @click="submit">确 定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { changePwd } from '@/api/user'
export default {
  props: {
    form: {
      type: Object,
      default: () => {
        return {
          passwordOld: '',
          passwordNew: '',
          passwordNew2: ''
        }
      }
    },
    rules: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  methods: {
    resetPassword() {
      this.form.passwordOld = ''
      this.form.passwordNew = ''
      this.form.passwordNew2 = ''
    },
    submit() {
      this.$refs['ruleFormPasswordRef'].validate((valid) => {
        if (valid) {
          this.$confirm('确定修改密码？', '提示信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async() => {
              const tempData = Object.assign({}, this.form)
              await changePwd(tempData)
              this.$message({
                type: 'success',
                message: '修改密码成功!'
              })
            })
            .catch(err => { console.error(err) })
        }
      })
    }
  }
}
</script>

